﻿using gudusoft.gsqlparser;
using gudusoft.gsqlparser.nodes;
using gudusoft.gsqlparser.stmt;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {

        private string SUCCESS_MSG = "已匹配SQL：{0}条。处理成功：{1}条。";

        string ROW_EXPR= "\n";

        /// <summary>
        /// 
        /// 匹配时间正则表达式
        /// 
        /// '2021-11-04 18:54:11.03'
        /// @"'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d+'"
        /// 
        /// TO_DATE\('\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}','yyyy-mm-dd hh24:mi:ss'\)
        /// @"TO_DATE\('\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}','yyyy-mm-dd hh24:mi:ss'\)"
        /// 
        /// TIMESTAMP '2021-11-02 16:05:33'
        /// TIMESTAMP'2021-12-12 16:58:36.277'
        /// @"TIMESTAMP'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.?\d*'"
        /// 
        /// </summary>
        private List<string> DATETIME_PATTERN = new List<string>() 
        { 
            @"'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d+'", 
            @"TO_DATE\('\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}','yyyy-mm-dd hh24:mi:ss'\)", 
            @"TIMESTAMP'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.?\d*'" ,
            "sysdate",
            @"getDate\(\)",
            @"NOW\(\)"
        };

        public Form1()
        {
            InitializeComponent();
            button1.Click += Button1_Click;
            button2.Click += Button2_Click;
            button3.Click += Button3_Click;
            button4.Click += Button4_Click;
            button5.Click += Button5_Click;
        }

        /// <summary>
        /// 6.0平台 | 行SQL替换1000为${ADMIN_STAFF_COMPANYID}，删除保存在对应数据库脚本中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button5_Click(object sender, EventArgs e)
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "SQL files (*.sql)|*.sql";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string sqltexts = getFileContent();
                List<string> sqls = matchInsertSql(sqltexts);
                AdminInfo adminInfo = new AdminInfo();
                int count = processByDbtypeWithDelete(sqls, adminInfo, sqltexts);
                toolStripStatusLabel1.Text = string.Format(SUCCESS_MSG, sqls.Count, count);
            }
        }

        /// <summary>
        /// 6.0平台 | 全文SQL替换$(ADMIN_STAFF_ID)为1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button4_Click(object sender, EventArgs e)
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "SQL files (*.sql)|*.sql";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string sqltexts = getFileContent();

                string oracleSqltexts = string.Copy(sqltexts);
                string sqlserverSqltexts = string.Copy(sqltexts);
                string mysqlSqltexts = string.Copy(sqltexts);

                oracleSqltexts= ReplaceDateValueAndOther(oracleSqltexts,SAVE_TYPE.ORACLE);
                sqlserverSqltexts= ReplaceDateValueAndOther(sqlserverSqltexts, SAVE_TYPE.SQLSERVER);
                mysqlSqltexts= ReplaceDateValueAndOther(mysqlSqltexts, SAVE_TYPE.MYSQL);

                string outputDirectory = createCustomDir();
                saveSQLFile(oracleSqltexts, outputDirectory, SAVE_TYPE.ORACLE);
                saveSQLFile(sqlserverSqltexts, outputDirectory, SAVE_TYPE.SQLSERVER);
                saveSQLFile(mysqlSqltexts, outputDirectory, SAVE_TYPE.MYSQL);
                toolStripStatusLabel1.Text = "处理成功";
                Process.Start(outputDirectory);
            }
        }

        /// <summary>
        /// 根据正则表达式匹配并替换为时间函数
        /// </summary>
        /// <param name="sqltexts"></param>
        /// <param name="dbtype"></param>
        /// <returns></returns>
        private string ReplaceDateValueAndOther(string sqltexts,SAVE_TYPE dbtype)
        {
            foreach (var pattern in DATETIME_PATTERN)
            {
                MatchCollection mc = Regex.Matches(sqltexts, pattern,RegexOptions.IgnoreCase);
                if (mc.Count == 0)
                {
                    continue;
                }
                foreach (Match item in mc)
                {
                    sqltexts = sqltexts.Replace(item.Value, getDateFunction(dbtype));
                }
            }

            AdminInfo adminInfo = new AdminInfo();
            sqltexts = sqltexts.Replace(adminInfo.ADMIN_COMPANY_ID, "1000");
            sqltexts = sqltexts.Replace(adminInfo.ADMIN_DEPARTMENT_ID, "1");
            sqltexts = sqltexts.Replace(adminInfo.ADMIN_POSITION_ID, "1");
            sqltexts = sqltexts.Replace(adminInfo.ADMIN_STAFF_ID, "1");
            return sqltexts;
        }

        /// <summary>
        /// 6.0平台 | 行SQL替换1000为${ADMIN_STAFF_COMPANYID}
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "SQL files (*.sql)|*.sql";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string sqltexts = getFileContent();
                List<string> sqls = matchInsertSql(sqltexts);
                AdminInfo adminInfo = new AdminInfo();
                int count = processByDbtype(sqls, adminInfo, sqltexts);
                toolStripStatusLabel1.Text = string.Format(SUCCESS_MSG, sqls.Count, count);
            }
        }

        /// <summary>
        /// 6.0平台 | 行SQL替换$(ADMIN_STAFF_ID)为1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, EventArgs e)
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "SQL files (*.sql)|*.sql";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string sqltexts = getFileContent();
                List<string> sqls = matchInsertSql(sqltexts);
                AdminInfo adminInfo = new AdminInfo("1000","1","1","1");
                int count = processByDbtype(sqls, adminInfo, sqltexts);
                toolStripStatusLabel1.Text = string.Format(SUCCESS_MSG, sqls.Count, count);
            }
        }

        /// <summary>
        /// 5.0平台 | 行SQL替换ADMIN_STAFF_ID为1000
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button3_Click(object sender, EventArgs e)
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "SQL files (*.sql)|*.sql";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string sqltexts = getFileContent();
                List<string> inserts = matchInsertSql(sqltexts);
                AdminInfo adminInfo = new AdminInfo("1000", "1000", "1000", "1000");
                int count = processByDbtype(inserts, adminInfo, sqltexts);

                toolStripStatusLabel1.Text = string.Format(SUCCESS_MSG, inserts.Count, count);
            }
        }

        /// <summary>
        /// 按数据库处理SQL语句
        /// </summary>
        /// <param name="sqls">插入SQL</param>
        /// <param name="adminInfo">SQL替换值</param>
        /// <param name="sqltexts">原文件中SQL语句</param>
        private int processByDbtype(List<string> sqls, AdminInfo adminInfo, string sqltexts)
        {
            string outputDirectory = createCustomDir();
            List<string> list = getNewInsertSqlAndSave(sqls, adminInfo, outputDirectory, SAVE_TYPE.ORACLE, sqltexts);
            getNewInsertSqlAndSave(sqls, adminInfo, outputDirectory, SAVE_TYPE.SQLSERVER, sqltexts);
            getNewInsertSqlAndSave(sqls, adminInfo, outputDirectory, SAVE_TYPE.MYSQL, sqltexts);
            getNewDeleteSqlAndSave(sqls, adminInfo, outputDirectory, SAVE_TYPE.DELETE, sqltexts);
            Process.Start(outputDirectory);
            return list.Count;
        }

        /// <summary>
        /// 按数据库处理SQL语句
        /// </summary>
        /// <param name="sqls">插入SQL</param>
        /// <param name="adminInfo">SQL替换值</param>
        /// <param name="sqltexts">原文件中SQL语句</param>
        private int processByDbtypeWithDelete(List<string> sqls, AdminInfo adminInfo, string sqltexts)
        {
            string outputDirectory = createCustomDir();
            List<string> list = getNewInsertDeleteSqlAndSave(sqls, adminInfo, outputDirectory, SAVE_TYPE.DELETE_AND_ORACLE, sqltexts);
            getNewInsertDeleteSqlAndSave(sqls, adminInfo, outputDirectory, SAVE_TYPE.DELETE_AND_SQLSERVER, sqltexts);
            getNewInsertDeleteSqlAndSave(sqls, adminInfo, outputDirectory, SAVE_TYPE.DELETE_AND_MYSQL, sqltexts);
            Process.Start(outputDirectory);
            return list.Count;
        }

        /// <summary>
        /// 处理插入SQL并保存
        /// </summary>
        /// <param name="sqls"></param>
        /// <param name="adminInfo"></param>
        /// <param name="outputDirectory"></param>
        /// <param name="dbtype"></param>
        /// <param name="sqltexts">原始SQL文本</param>
        /// <returns></returns>
        private List<string> getNewInsertSqlAndSave(List<string> sqls, AdminInfo adminInfo, string outputDirectory, SAVE_TYPE dbtype, string sqltexts)
        {
            List<string> newSqls = sqls.Select(v => getNewInsertSql(v, dbtype, adminInfo)).Where(v => !string.IsNullOrEmpty(v)).ToList();
            if (newSqls.Count > 0)
            {
                saveSQLFile(getSaveText(newSqls, sqls, sqltexts), outputDirectory, dbtype);
            }

            return newSqls;
        }

        /// <summary>
        /// 处理插入SQL并加上删除SQL并保存
        /// </summary>
        /// <param name="sqls"></param>
        /// <param name="adminInfo"></param>
        /// <param name="outputDirectory"></param>
        /// <param name="dbtype"></param>
        /// <param name="sqltexts"></param>
        /// <returns></returns>
        private List<string> getNewInsertDeleteSqlAndSave(List<string> sqls, AdminInfo adminInfo, string outputDirectory, SAVE_TYPE dbtype, string sqltexts)
        {
            List<string> newDeleteSqls = sqls.Select(v => getNewDeleteSql(v, dbtype)).Where(v => !string.IsNullOrEmpty(v)).ToList();
            List<string> newInsertSqls = sqls.Select(v => getNewInsertSql(v, dbtype, adminInfo)).Where(v => !string.IsNullOrEmpty(v)).ToList();
            if (newInsertSqls.Count > 0)
            {
                String del_s = getSaveText(newDeleteSqls, sqls, sqltexts);
                String ins_s = getSaveText(newInsertSqls, sqls, sqltexts);
                saveSQLFile(del_s + ROW_EXPR + ins_s, outputDirectory, dbtype);
            }

            return newInsertSqls;
        }

        /// <summary>
        /// 获取新的删除SQL
        /// </summary>
        /// <param name="sqls"></param>
        /// <param name="adminInfo"></param>
        /// <param name="outputDirectory"></param>
        /// <param name="dbtype"></param>
        /// <param name="sqltexts"></param>
        /// <returns></returns>
        private List<string> getNewDeleteSqlAndSave(List<string> sqls, AdminInfo adminInfo, string outputDirectory, SAVE_TYPE dbtype, string sqltexts)
        {
            List<string> newSqls = sqls.Select(v => getNewDeleteSql(v, dbtype)).Where(v => !string.IsNullOrEmpty(v)).ToList();
            if (newSqls.Count > 0)
            {
                saveSQLFile(getSaveText(newSqls, sqls, sqltexts), outputDirectory, dbtype);
            }

            return newSqls;
        }

        /// <summary>
        /// 获取处理后要保存文件
        /// </summary>
        /// <param name="newSqls">处理后文本</param>
        /// <param name="inserts">处理前文本</param>
        /// <param name="sqltexts">原始输入文本</param>
        /// <returns></returns>
        private string getSaveText(List<string> newSqls, List<string> inserts, string sqltexts)
        {
            for (int i = 0; i < inserts.Count; i++)
            {
                sqltexts = sqltexts.Replace(inserts[i], newSqls[i]);
            }
            return sqltexts;
        }

        /// <summary>
        /// 获取选中文件内容
        /// </summary>
        /// <returns>文件内容</returns>
        private string getFileContent()
        {
            string sqltexts;
            try
            {
                using (StreamReader r = new StreamReader(openFileDialog1.FileName))
                {
                    sqltexts = r.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return string.Empty;
            }

            //替换掉数据库前缀
            sqltexts = sqltexts.Replace("ADP.dbo.AA_", "");
            sqltexts = sqltexts.Replace("ADP.dbo.AB_", "");
            sqltexts = sqltexts.Replace("ADP.dbo.BB_", "");
            sqltexts = sqltexts.Replace("ADP.dbo.CC_", "");

            return sqltexts;
        }

        /// <summary>
        /// 新建输出SQL文件夹
        /// </summary>
        /// <returns>输出SQL文件夹</returns>
        private string createCustomDir()
        {
            string directory1 = Path.GetDirectoryName(openFileDialog1.FileName) + @"\" + DateTime.Now.ToString("yyyy-MM-dd-hhmmss");
            if (!Directory.Exists(directory1))
            {
                Directory.CreateDirectory(directory1);
            }
            return directory1;
        }

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="sqlText">保存的文本内容</param>
        /// <param name="outputDirectory">输出文件夹</param>
        /// <param name="dbType">输出数据库类型</param>
        private void saveSQLFile(string sqlText, string outputDirectory, SAVE_TYPE dbType)
        {
            //string sqlText = string.Join("\r\n", inserts);
            try
            {
                //Pass the filepath and filename to the StreamWriter Constructor

                string filename = string.Empty;
                switch (dbType)
                {
                    case SAVE_TYPE.ORACLE:
                    case SAVE_TYPE.DELETE_AND_ORACLE:
                        filename = "oracle.sql";
                        break;
                    case SAVE_TYPE.SQLSERVER:
                    case SAVE_TYPE.DELETE_AND_SQLSERVER:
                        filename = "sqlserver.sql";
                        break;
                    case SAVE_TYPE.MYSQL:
                    case SAVE_TYPE.DELETE_AND_MYSQL:
                        filename = "mysql.sql";
                        break;
                    case SAVE_TYPE.DELETE:
                        filename = "delete.sql";
                        break;
                    case SAVE_TYPE.OTHER:
                        filename = "output.sql";
                        break;
                    default:
                        break;
                }

                string targetFile = outputDirectory + @"\" + filename;
                StreamWriter sw = new StreamWriter(targetFile);
                //Write a line of text
                sw.WriteLine(sqlText);
                //Close the file
                sw.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);

            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }

        /// <summary>
        /// 正则表达式匹配插入行
        /// </summary>
        /// <param name="sqltexts">文本内容</param>
        /// <returns>插入行列表</returns>
        private List<string> matchInsertSql(string sqltexts)
        {
            List<string> inserts = new List<string>();
            string expr = ROW_EXPR;
            string[] list = Regex.Split(sqltexts, expr, RegexOptions.Multiline);
            foreach (var item in list)
            {
                if (string.IsNullOrWhiteSpace(item))
                {
                    continue;
                }
                if (!item.ToLower().Contains("insert"))
                {
                    continue;
                }
                inserts.Add(item);
            }

            return inserts;
        }

        /// <summary>
        /// 单行插入语句处理
        /// </summary>
        /// <param name="sqltext">SQL语句</param>
        /// <param name="dbtype">数据库类型</param>
        /// <param name="adminInfo">数据替换值</param>
        /// <returns>处理后SQL语句</returns>
        private string getNewInsertSql(string sqltext, SAVE_TYPE dbtype, AdminInfo adminInfo)
        {
            TInsertSqlStatement insert = getSqlStatement(sqltext);
            List<string> clist = getColumnList(insert);
            List<string> vlist = new List<string>();
            bool flag = getColumns(sqltext, dbtype, insert, clist, vlist);
            if (!flag)
            {
                return sqltext;
            }
            for (int i = 0; i < clist.Count; i++)
            {
                setInsertValue(dbtype, clist, vlist, i, adminInfo);
            }

            //过滤出值不为空的
            List<string> clistNew = new List<string>();
            List<string> vlistNew = new List<string>();
            getColumnValueSkipNull(clist, vlist, clistNew, vlistNew);
            string newSqltext = $"INSERT INTO {insert.TargetTable.FullName}({string.Join(",", clistNew)}) VALUES({string.Join(",", vlistNew)});";
            //Console.WriteLine(newSqltext);
            return newSqltext;
        }

        /// <summary>
        /// 获取单条删除SQL
        /// </summary>
        /// <param name="sqltext"></param>
        /// <param name="dbtype"></param>
        /// <returns></returns>
        private string getNewDeleteSql(string sqltext, SAVE_TYPE dbtype)
        {
            TInsertSqlStatement insert = getSqlStatement(sqltext);
            List<string> clist = getColumnList(insert);
            List<string> vlist = new List<string>();
            bool flag = getColumns(sqltext, dbtype, insert, clist, vlist);
            if (!flag)
            {
                return sqltext;
            }
            string id = getDeleteId(clist, vlist);
            string newSqltext = $"DELETE FROM {insert.TargetTable.FullName} WHERE ID = {id};";
            return newSqltext;
        }

        /// <summary>
        /// 获取列
        /// </summary>
        /// <param name="sqltext"></param>
        /// <param name="dbtype"></param>
        /// <param name="insert"></param>
        /// <param name="clist"></param>
        /// <param name="vlist"></param>
        /// <returns>是否出现异常情况</returns>
        private bool getColumns(string sqltext, SAVE_TYPE dbtype, TInsertSqlStatement insert, List<string> clist, List<string> vlist)
        {
            try
            {
                getValueList(insert, vlist);
            }
            catch (Exception ex)
            {
                if (dbtype == SAVE_TYPE.ORACLE)
                {
                    MessageBox.Show($"该行SQL语句不能处理：{insert.String}{ex.Message}");
                }
                return false;
            }
            if (clist.Count != vlist.Count)
            {
                if (dbtype == SAVE_TYPE.ORACLE)
                {
                    MessageBox.Show($"该行SQL语句处理失败：{sqltext}");
                }
                return false;
            }
            return true;
        }



        /// <summary>
        /// 匹配SQL
        /// </summary>
        /// <param name="sqltext"></param>
        /// <returns></returns>
        private static TInsertSqlStatement getSqlStatement(string sqltext)
        {
            TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvmssql);
            //sqlparser.sqltext = "insert into emp e1 (e1.lastname,job) values('scott',10);";
            sqlparser.sqltext = sqltext;
            sqlparser.parse();

            TInsertSqlStatement insert = (TInsertSqlStatement)sqlparser.sqlstatements.get(0);
            return insert;
        }

        /// <summary>
        /// 获取删除Id
        /// </summary>
        /// <param name="clist"></param>
        /// <param name="vlist"></param>
        /// <returns></returns>
        private string getDeleteId(List<string> clist, List<string> vlist)
        {
            string id = string.Empty;
            //取出Id的值
            for (int i = 0; i < vlist.Count; i++)
            {
                if (clist[i] == null)
                {
                    continue;
                }
                if (!clist[i].ToLower().Equals("id"))
                {
                    continue;
                }
                id = vlist[i];
                break;
            }
            return id;
        }

        /// <summary>
        /// 获取值不为空的列和值
        /// </summary>
        /// <param name="clist">列名</param>
        /// <param name="vlist">值</param>
        /// <param name="clistNew">过滤后列名</param>
        /// <param name="vlistNew">过滤后值</param>
        private static void getColumnValueSkipNull(List<string> clist, List<string> vlist, List<string> clistNew, List<string> vlistNew)
        {
            for (int i = 0; i < vlist.Count; i++)
            {
                if ("NULL".Equals(vlist[i]))
                {
                    continue;
                }
                clistNew.Add(clist[i]);
                vlistNew.Add(vlist[i]);
            }
        }

        /// <summary>
        /// 设置插入行值类型
        /// </summary>
        /// <param name="dbtype">数据库类型</param>
        /// <param name="clist">插入列名</param>
        /// <param name="vlist">插入值</param>
        /// <param name="i">插入序号</param>
        /// <param name="adminInfo">数据替换值</param>
        private void setInsertValue(SAVE_TYPE dbtype, List<string> clist, List<string> vlist, int i, AdminInfo adminInfo)
        {

            bool vdateFlag = isDateTime(clist[i], vlist[i]);
            bool cflag = isCompany(clist[i]);
            bool dflag = isDepartment(clist[i]);
            bool pflag = isPosition(clist[i]);
            bool sflag = isStaff(clist[i]);
            //bool flagN = vlist[i].StartsWith("N'") && vlist[i].EndsWith("'");

            if (vdateFlag)
            {
                vlist[i] = getDateFunction(dbtype);
            }
            if (cflag)
            {
                vlist[i] = adminInfo.ADMIN_COMPANY_ID;
            }
            if (dflag)
            {
                vlist[i] = adminInfo.ADMIN_DEPARTMENT_ID;
            }
            if (pflag)
            {
                vlist[i] = adminInfo.ADMIN_POSITION_ID;
            }
            if (sflag)
            {
                vlist[i] = adminInfo.ADMIN_STAFF_ID;
            }
            //if (flagN)
            //{
            //    vlist[i] = vlist[i].Substring(1);
            //}
        }

        /// <summary>
        /// 根据数据库类型返回时间函数
        /// </summary>
        /// <param name="dbtype"></param>
        /// <returns></returns>
        private string getDateFunction(SAVE_TYPE dbtype)
        {
            string result = "";
            switch (dbtype)
            {
                case SAVE_TYPE.ORACLE:
                case SAVE_TYPE.DELETE_AND_ORACLE:
                    result = "sysdate";
                    break;
                case SAVE_TYPE.SQLSERVER:
                case SAVE_TYPE.DELETE_AND_SQLSERVER:
                    result = "getDate()";
                    break;
                case SAVE_TYPE.MYSQL:
                case SAVE_TYPE.DELETE_AND_MYSQL:
                    result = "NOW()";
                    break;
                default:
                    break;
            }
            return result;
        }

        /// <summary>
        /// 获取插入列名
        /// </summary>
        /// <param name="insert">插入数据</param>
        /// <returns>列名列表</returns>
        private static List<string> getColumnList(TInsertSqlStatement insert)
        {
            List<String> cclist = new List<string>();
            foreach (var item in insert.ColumnList)
            {
                cclist.Add(item.ToString());
            }

            return cclist;
        }


        /// <summary>
        /// 获取值列表
        /// </summary>
        /// <param name="insert">插入对象</param>
        /// <param name="vlist">值对象</param>
        /// <returns>值列表</returns>
        private void getValueList(TInsertSqlStatement insert, List<string> vlist)
        {
            //List<string> vlist = new List<string>();
            StringBuilder builder = new StringBuilder();
            foreach (TSourceToken t in insert.Values.SourceTokenList)
            {
                string v = t.ToString();
                if (v.Equals("(") || v.Equals(")") || v.Equals(",") || string.IsNullOrWhiteSpace(v) || "TO_CLOB".Equals(v.ToUpper()))
                {
                    if (!string.IsNullOrWhiteSpace(builder.ToString()))
                    {
                        vlist.Add(builder.ToString());
                        builder.Remove(0, builder.Length);
                    }
                    continue;
                }
                builder.Append(v);
            }
            //return vlist;

        }

        /// <summary>
        /// 是不是时间类型
        /// </summary>
        /// <param name="v">插入值</param>
        /// <returns>是不是时间类型</returns>
        private bool isDateTime(string c, string v)
        {
            if (string.IsNullOrEmpty(v))
            {
                return false;
            }
            bool flag = false;
            if (c.Equals("MODIFY_TIME") || c.Equals("CREATE_TIME"))
            {
                flag = true;
            }

            foreach (string pattern in DATETIME_PATTERN)
            {
                MatchCollection mc = Regex.Matches(v, pattern);
                if (mc.Count > 0)
                {
                    flag = true;
                    break;
                }
            }

            if (v.ToLower().Equals("sysdate"))
            {
                flag = true;
            }
            if (v.ToLower().Equals("getdate()"))
            {
                flag = true;
            }
            if (v.ToLower().Equals("now()"))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 当前列是员工
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        private bool isStaff(String c)
        {
            return c.Equals("MODIFY_STAFF_ID") | c.Equals("CREATE_STAFF_ID") | c.Equals("OWNER_STAFF_ID");
        }

        /// <summary>
        /// 当前列是公司
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        private bool isCompany(String c)
        {
            return c.Equals("CID");
        }

        /// <summary>
        /// 当前列是岗位
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        private bool isPosition(String c)
        {
            return c.Equals("OWNER_POSITION_ID") | c.Equals("CREATE_POSITION_ID");
        }

        /// <summary>
        /// 当前列是部门
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        private bool isDepartment(String c)
        {
            return c.Equals("OWNER_DEPARTMENT_ID") | c.Equals("CREATE_DEPARTMENT_ID");
        }

        /// <summary>
        /// 数据库类型
        /// </summary>
        enum SAVE_TYPE
        {
            ORACLE,
            SQLSERVER,
            MYSQL,
            DELETE_AND_ORACLE,
            DELETE_AND_SQLSERVER,
            DELETE_AND_MYSQL,
            DELETE,
            OTHER
        }

        public class AdminInfo
        {

            public string ADMIN_COMPANY_ID;

            public string ADMIN_DEPARTMENT_ID;

            public string ADMIN_POSITION_ID;

            public string ADMIN_STAFF_ID;

            public AdminInfo()
            {
                this.ADMIN_COMPANY_ID = "${ADMIN_COMPANY_ID}";
                this.ADMIN_DEPARTMENT_ID = "${ADMIN_DEPARTMENT_ID}";
                this.ADMIN_POSITION_ID = "${ADMIN_POSITION_ID}";
                this.ADMIN_STAFF_ID = "${ADMIN_STAFF_ID}";
            }

            public AdminInfo(string ADMIN_COMPANY_ID, string ADMIN_DEPARTMENT_ID, string ADMIN_POSITION_ID, string ADMIN_STAFF_ID)
            {
                this.ADMIN_COMPANY_ID = ADMIN_COMPANY_ID;
                this.ADMIN_DEPARTMENT_ID = ADMIN_DEPARTMENT_ID;
                this.ADMIN_POSITION_ID = ADMIN_POSITION_ID;
                this.ADMIN_STAFF_ID = ADMIN_STAFF_ID;
            }

        }


    }
}
